/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.constant.enums;

import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 要素库表类型枚举
 *
 * @author hxloongs
 * @date 2024-9-24
 */
@AllArgsConstructor
@Getter
public enum IsfdMetadataTableNameEnum {
    MAT_MATERIAL("mat_material", "结构cae-金属材料", "材料库"),
    COMP_MATERIAL("comp_material", "结构cae-复合材料", "材料库"),
    ISFD_METAL_MATERIAL("isfd_metal_material", "参数化建模-金属材料", "材料库"),
    ISFD_COMPOSITE_MATERIAL("isfd_composite_material", "参数化建模-复合材料", "材料库"),
    ISFD_LAYUP_METAL_CASE_META("isfd_layup_metal_case_meta", "参数化建模-金属壳元属性", "材料库"),
    FASTENER_EXPORTDATA("fastener_exportdata", "紧固件表", "紧固件库"),
    ANALYSIS_LIST("analysis_list", "算法表", "算法库"),
    OPERATOR_LIST("operator_list", "算子表", "算法库"),
    ANALYSIS_PARAMETER_CONFIG("analysis_parameter_config", "算法参数表", "参数库"),
    OPERATOR_PARAMETER_CONFIG("operator_parameter_config", "算子参数表", "参数库"),
    ISFD_MODEL("isfd_model", "模型表", "模型库"),
    ISFD_RESULT("isfd_result", "结果表", "结果库"),
    ISFD_SECTION("isfd_section", "型材表", "型材库"),
    ISFD_LAYUP_SKIN_PROPERTIE("isfd_layup_skin_propertie", "铺层表", "铺层库"),
    ISFD_LOAD("isfd_load", "载荷表", "载荷库"),
    ISFD_CALCULATION_EXAMPLE("isfd_calculation_example", "算例表", "算例库"),

    ISFD_SECTION_RESULT("isfd_section_result", "型材结果表", "型材库结果"),
    ISFD_LAYUP_EQUIVALENT_MATERIAL("isfd_layup_equivalent_material", "铺层库结果表-对应等效材料", "铺层库结果");

    private final String value;
    private final String databaseName;
    private final String databaseType;

    public static final Integer LEVEL_FOUR = 4;
    public static final Integer LEVEL_FIVE = 5;

    /**
     * 根据表名获取中文表名
     */
    public static String getDescByValue(String value) {
        IsfdMetadataTableNameEnum isfdMetadataTableNameEnum = forValue(value);
        if (isfdMetadataTableNameEnum == null) {
            return null;
        }
        return isfdMetadataTableNameEnum.getDatabaseName();
    }

    /**
     * 根据表名获取枚举
     */
    public static IsfdMetadataTableNameEnum forValue(String value) {
        if (StringUtils.isBlank(value)) {
            return null;
        }
        for (IsfdMetadataTableNameEnum isfdMetadataTableNameEnum : IsfdMetadataTableNameEnum.values()) {
            if (isfdMetadataTableNameEnum.getValue().equals(value)) {
                return isfdMetadataTableNameEnum;
            }
        }
        return null;
    }

    /**
     * 根据中文表名获取枚举
     */
    public static IsfdMetadataTableNameEnum forDesc(String desc) {
        if (desc == null) {
            return null;
        }
        for (IsfdMetadataTableNameEnum isfdMetadataTableNameEnum : IsfdMetadataTableNameEnum.values()) {
            if (desc.equals(isfdMetadataTableNameEnum.getDatabaseName())) {
                return isfdMetadataTableNameEnum;
            }
        }
        return null;
    }

    /**
     * 根据数据库名称获取枚举
     * 注意这里可能返回多个，当前是匹配到一个就返回了，不影响后面业务，所以没有做更多的集合逻辑
     */
    public static IsfdMetadataTableNameEnum forDatabaseType(String databaseType) {
        if (StringUtils.isBlank(databaseType)) {
            return null;
        }
        for (IsfdMetadataTableNameEnum isfdMetadataTableNameEnum : IsfdMetadataTableNameEnum.values()) {
            if (databaseType.equals(isfdMetadataTableNameEnum.getDatabaseType())) {
                return isfdMetadataTableNameEnum;
            }
        }
        return null;
    }

    /**
     * 根据数据库类型返回关系有几个节点level
     */
    public static Integer checkOutDatabaseTypeAndReturnLevel(String databaseType) {
        IsfdMetadataTableNameEnum isfdMetadataTableNameEnum = forDatabaseType(databaseType);
        if (isfdMetadataTableNameEnum == null) {
            throw new BaseException("表枚举中不存在该表，请确认后重试！");
        }
        // 当前只有铺层和型材的level是5层
        if (IsfdMetadataTableNameEnum.ISFD_SECTION == isfdMetadataTableNameEnum ||
            IsfdMetadataTableNameEnum.ISFD_LAYUP_SKIN_PROPERTIE == isfdMetadataTableNameEnum) {
            return LEVEL_FIVE;
        }
        return LEVEL_FOUR;
    }

    /**
     * 根据表名和节点level数，计算到底是返回结果tableName还是就返回元数据表tableName
     * 当前只有型材和铺层有结果表
     */
    public static String getTableNameByLevel(String tableName, Integer level) {
        if (LEVEL_FOUR.equals(level)) {
            return tableName;
        }
        if (IsfdMetadataTableNameEnum.ISFD_SECTION == IsfdMetadataTableNameEnum.forValue(tableName)) {
            return IsfdMetadataTableNameEnum.ISFD_SECTION_RESULT.getValue();
        }

        if (IsfdMetadataTableNameEnum.ISFD_LAYUP_SKIN_PROPERTIE == IsfdMetadataTableNameEnum.forValue(tableName)) {
            return IsfdMetadataTableNameEnum.ISFD_LAYUP_EQUIVALENT_MATERIAL.getValue();
        }
        return tableName;
    }
}
